﻿using System;
using System.Collections.Generic;
using System.Text;
using com.crawler.common;
using System.Text.RegularExpressions;
using System.Web;

namespace com.crawler.components
{
    public class BaiDuChapter
    {
        /// <summary>
        /// 触发发送消息的事件
        /// </summary>
        public event SendMsgEventHandler SendMsg;
        private static object m_syn_obj = new object();

        DBManager m_db_mgr = new DBManager();

        /// <summary>
        /// 站点列表
        /// </summary>
        public  List<BookSiteInfo> m_list = null;

        /// <summary>
        /// 系统配置
        /// </summary>
        public SysConfigInfo m_sys_config = null;

        #region 消息回送
        /// <summary>
        /// 发送消息到客户端
        /// </summary>
        /// <param name="i_msg"></param>
        private void SendMsg2Client(object i_msg)
        {
            SendMsgEventArgs e = new SendMsgEventArgs();
            e.Msg = i_msg;
            if (SendMsg != null)
            {
                SendMsg(this, e);
            }

        }
        #endregion

        ChapterContentManager m_chapter_mgr = new ChapterContentManager();

        /// <summary>
        /// 匹配章节内容正在
        /// </summary>
        private string m_chapter_content_reg = "<(?=div|table|td)[^>]*?(?=booktext|bookcontent|readtxt)[^>]*?>";

        /// <summary>
        /// 匹配html标签
        /// </summary>
        private string m_html_tag_reg = "<[/]*(?=div|table|td)[^>]*?>";

        /// <summary>
        /// 章节图片的正则
        /// </summary>
        private string m_img_reg = "<img[^<>]*?src=s*[\'\"]*([^\"\']*\\d+/\\d+/\\d+[^\"\']*)[\'\"]*[^<>]*?>";

        /// <summary>
        /// 匹配章节地址正则
        /// </summary>
        private string m_chapter_reg = "<a[^<>]*?href=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>(.*?)</a>";

        /// <summary>
        /// 搜索章节列表
        /// </summary>
        public void StartBook(BookInfo i_book)
        {
            lock (m_syn_obj)
            {
                try
                {
                    i_book.ChapterListUrl = Regex.Replace(i_book.ChapterListUrl, "\\/(\\d+)\\.qidian\\.com", "/www.qidian.com", RegexOptions.IgnoreCase);
                    i_book.ChapterListUrl = Regex.Replace(i_book.ChapterListUrl, "\\/(\\d+)\\.qdmm\\.com", "/www.qdmm.com", RegexOptions.IgnoreCase);
                    string t_html = HttpHelper.GetHttpRequestStringByNUll_Post(i_book.ChapterListUrl);

                    if (!string.IsNullOrEmpty(t_html))
                    {
                        if (t_html==("404"))
                        {
                            LogHelper.Info(i_book.ChapterListUrl + "已经被删除");
                            m_db_mgr.UpdateBookUrl(i_book.BookId, "");
                            return;
                        }

                        List<BookSiteInfo> t_site_list = m_list.FindAll(delegate(BookSiteInfo t_site_info)
                        {
                            return t_site_info.SiteId == i_book.SiteId;
                        });

                        if (t_site_list == null || t_site_list.Count <= 0)
                            return;

                        MatchCollection t_ma = Regex.Matches(t_html, t_site_list[0].ChapterListReg, RegexOptions.IgnoreCase | RegexOptions.Compiled);

                        if (t_ma == null)
                            return;

                        ChapterInfo t_chpater = null;

                        int t_index = 0;

                        int t_total_count = t_ma.Count;

                        List<ChapterInfo> t_list_chapter = new List<ChapterInfo>();

                        DateTime t_update_time_now = DateTime.Now;

                        if (t_ma != null && t_ma.Count > 0)
                        {
                            t_update_time_now = DateTime.Now.AddSeconds(t_ma.Count * -4);
                        }

                        foreach (Match t_mc in t_ma)
                        {
                            t_chpater = new ChapterInfo();

                            if (t_mc.Groups.Count >= t_site_list[0].ChapterNameRegIndex)
                            {
                                t_chpater.ChapterName = t_mc.Groups[t_site_list[0].ChapterNameRegIndex].Value.ToString().Trim();
                            }
                            if (t_mc.Groups.Count >= t_site_list[0].ChapterUrlRegIndex)
                            {
                                t_chpater.ChapterUrl = CrawlerUtil.GetFullUrl(i_book.ChapterListUrl, t_mc.Groups[t_site_list[0].ChapterUrlRegIndex].Value.ToString().Trim());
                            }

                            if (t_mc.Groups.Count >= t_site_list[0].UpdateTimeRegIndex && t_site_list[0].UpdateTimeRegIndex != -1)
                            {
                                DateTime t_update_time = DateTime.Now;
                                if (!DateTime.TryParse(t_mc.Groups[t_site_list[0].UpdateTimeRegIndex].Value.ToString(), out t_update_time))
                                    t_update_time = DateTime.Now;
                                t_chpater.UpdateTime = t_update_time;
                            }
                            else
                            {
                                t_index++;
                                //升序
                                if (t_site_list[0].ChapterOrder == ChapterOrderType.ASC)
                                {
                                    t_chpater.UpdateTime = t_update_time_now.AddSeconds(t_index * 3);
                                }
                                else if (t_site_list[0].ChapterOrder == ChapterOrderType.DESC)
                                {
                                    t_chpater.UpdateTime = t_update_time_now.AddSeconds(3 * (t_total_count + 5)).AddSeconds(t_index * (-3));
                                }
                            }

                            if (string.IsNullOrEmpty(t_chpater.ChapterName))
                                continue;

                            t_list_chapter.Add(t_chpater);

                        }

                        if (t_list_chapter.Count > 0)
                        {
                            t_list_chapter.Sort(delegate(ChapterInfo t_a, ChapterInfo t_b)
                            {
                                if (t_a.UpdateTime > t_b.UpdateTime)
                                    return 1;
                                else if (t_a.UpdateTime == t_b.UpdateTime)
                                    return 0;
                                return -1;
                            });
                        }

                        SendMsg2Client(string.Format("{0} 收录到{1}个章节", i_book.BookName, t_list_chapter.Count));
                        m_db_mgr.AddChapterList(i_book.BookId, i_book.BookName, t_list_chapter,i_book.Status , i_book.SiteId, i_book.SiteName);
                        //搜索章节内容
                       // StartSEOChatper(i_book.BookId);

                        //设置为已点击
                        //if (i_book.IsVIP)
                        //{
                        //    m_db_mgr.AddBookClick(i_book.BookId);

                        //    //下载封面
                        //    if (!string.IsNullOrEmpty(i_book.ICONUrl))
                        //    {
                        //        NetSiteCatchManager.DownBookICON(i_book.ICONUrl, i_book.BookId, m_sys_config.ICONPath);
                        //    }
                        //}
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("从搜索引擎搜索小说出现异常" + ex.ToString());
                }
            }
        }

        /// <summary>
        /// 搜索小说章节
        /// </summary>
        public void StartSEOChatper(long i_book_id,int i_site_id)
        {
            lock (m_syn_obj)
            {
                m_chapter_mgr.m_list = m_list;
                m_chapter_mgr.m_sys_config = m_sys_config;


                try
                {
                    List<ChapterInfo> t_chapter_list = m_db_mgr.GetChapterByBookId(i_book_id);

                    if (t_chapter_list == null || t_chapter_list.Count<=0)
                    {
                        SendMsg2Client(string.Format("{0} 没有章节需要收录内容", i_book_id));
                        return;
                    }
                    else
                    {
                        SendMsg2Client(string.Format("{0} 需要收录{1}个章节内容", i_book_id, t_chapter_list.Count));
                    }

                    //普通章节
                    List<ChapterInfo> t_normal_ch = t_chapter_list.FindAll(delegate(ChapterInfo t_ch)
                    {
                        return t_ch.ChapterUrl.ToLower().IndexOf("vip") < 0 && t_ch.IsDownLoad != Consts.ExistChapterContentBook;
                    });
                    
                    int t_normal_count = 0;
                    if (t_normal_ch != null && t_normal_ch.Count > 0)
                        t_normal_count = t_normal_ch.Count;

                    int t_normal_downloaded_count = SearchNormalChapter(i_site_id, i_book_id, t_normal_ch);

                    LogHelper.Info(string.Format("bookId={0} 需要下载 {1} 个普通章节，实际下载了 {2} 个章节", i_book_id, t_normal_count, t_normal_downloaded_count));

                    //vip章节
                    List<ChapterInfo> t_vip_ch = t_chapter_list.FindAll(delegate(ChapterInfo t_ch)
                    {
                        return t_ch.ChapterUrl.ToLower().IndexOf("vip") > -1 && t_ch.IsDownLoad != Consts.ExistChapterContentBook;
                    });

                    int t_vip_count = 0;

                    if (t_vip_ch != null && t_vip_ch.Count > 0)
                        t_vip_count = t_vip_ch.Count;

                    int t_vip_downloaded_count = SearchVIPChapter(i_book_id, t_vip_ch);

                    LogHelper.Info(string.Format("bookId={0} 需要下载 {1} 个VIP章节，实际下载了 {2} 个章节", i_book_id, t_vip_count, t_vip_downloaded_count));

                }
                catch (Exception ex)
                {
                    LogHelper.Error("搜索小说章节失败" + ex.ToString());
                }
            }
        }

        /// <summary>
        /// 搜索小说章节
        /// </summary>
        public void StartSEOChatperByCrawler(long i_book_id, int i_site_id)
        {
            lock (m_syn_obj)
            {
                m_chapter_mgr.m_list = m_list;
                m_chapter_mgr.m_sys_config = m_sys_config;

                LogHelper.Info(string.Format("百度搜索小说{0}的章节", i_book_id));
                try
                {
                    List<ChapterInfo> t_chapter_list = m_db_mgr.GetChapterByBookId(i_book_id);

                    if (t_chapter_list == null || t_chapter_list.Count <= 0)
                    {
                        SendMsg2Client(string.Format("{0} 没有章节需要收录内容", i_book_id));
                        return;
                    }
                    else
                    {
                        SendMsg2Client(string.Format("{0} 需要收录{1}个章节内容", i_book_id, t_chapter_list.Count));
                    }

                    //普通章节
                    List<ChapterInfo> t_normal_ch = t_chapter_list.FindAll(delegate(ChapterInfo t_ch)
                    {
                        return t_ch.ChapterUrl.ToLower().IndexOf("vip") < 0 && t_ch.IsDownLoad != Consts.ExistChapterContentBook;
                    });

                    int t_normal_count = 0;
                    if (t_normal_ch != null && t_normal_ch.Count > 0)
                        t_normal_count = t_normal_ch.Count;

                    int t_normal_downloaded_count = SearchNormalChapter(i_site_id, i_book_id, t_normal_ch);

                    LogHelper.Info(string.Format("bookId={0} 需要下载 {1} 个普通章节，实际下载了 {2} 个章节", i_book_id, t_normal_count, t_normal_downloaded_count));

                    //vip章节
                    List<ChapterInfo> t_vip_ch = t_chapter_list.FindAll(delegate(ChapterInfo t_ch)
                    {
                        return t_ch.ChapterUrl.ToLower().IndexOf("vip") > -1 && t_ch.IsDownLoad != Consts.ExistChapterContentBook;
                    });

                    int t_vip_count = 0;

                    if (t_vip_ch != null && t_vip_ch.Count > 0)
                        t_vip_count = t_vip_ch.Count;

                    int t_vip_downloaded_count = 0;

                    string t_chapter_content=string.Empty;

                    int t_start_index = 0;
                    string t_start_sign = string.Empty;
                    string t_end_sign = string.Empty;
                    string t_chapter_url = string.Empty;
                    string t_chapter_list_url = string.Empty;

                    foreach (ChapterInfo t_vip_chapter in t_vip_ch)
                    {
                        //从百度去搜索章节内容
                       t_chapter_content= m_chapter_mgr.SearchChapterContent(
                            t_vip_chapter.BookName,
                            t_vip_chapter.ChapterName,
                            t_vip_chapter.BookId,
                            t_vip_chapter.ChapterId,
                            out t_start_index,
                            out t_start_sign,
                            out t_end_sign,
                            out t_chapter_url,
                            out t_chapter_list_url);

                       if (!string.IsNullOrEmpty(t_chapter_content))
                           t_vip_downloaded_count++;
                    }

                    LogHelper.Info(string.Format("百度搜索小说 bookId={0} 需要下载 {1} 个VIP章节，实际下载了 {2} 个章节", i_book_id, t_vip_count, t_vip_downloaded_count));

                }
                catch (Exception ex)
                {
                    LogHelper.Error("搜索小说章节失败" + ex.ToString());
                }
            }
        }

        /// <summary>
        /// 搜索普通章节
        /// </summary>
        /// <param name="i_site_id"></param>
        /// <param name="i_book_id"></param>
        /// <param name="i_normal_chapter_list"></param>
        /// <returns></returns>
        private int SearchNormalChapter(int i_site_id, long i_book_id, List<ChapterInfo> i_normal_chapter_list)
        {
            if (i_normal_chapter_list == null || i_normal_chapter_list.Count == 0)
                return 0;

            List<BookSiteInfo> t_site_list = m_list.FindAll(delegate(BookSiteInfo t_site_info)
            {
                return t_site_info.SiteId == i_site_id;
            });

            if (t_site_list == null || t_site_list.Count <= 0)
                return 0;

            #region 普通章节

            string t_chapter_content = string.Empty;

            int t_downloaded_count = 0;

            foreach (ChapterInfo t_chapter in i_normal_chapter_list)
            {
                if (i_site_id == Consts.qidian || i_site_id == Consts.qdmm)
                {
                   t_chapter_content=  SearchNomalChapter(t_chapter);

                   if (!string.IsNullOrEmpty(t_chapter_content))
                       t_downloaded_count++;
                }
                else
                {
                    t_chapter_content = HttpHelper.GetHttpRequestStringByNUll_Post(t_chapter.ChapterUrl);

                    if (!string.IsNullOrEmpty(t_chapter_content))
                    {
                        Match t_mc = Regex.Match(t_chapter_content, t_site_list[0].ChapterContentReg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                        if (t_mc != null && t_mc.Groups.Count >= 0)
                        {
                            t_chapter_content = t_mc.Groups[1].Value.ToString();

                            if (!string.IsNullOrEmpty(t_site_list[0].ChapterContentFilterReg))
                            {
                                string[] t_filter_arr = t_site_list[0].ChapterContentFilterReg.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
                                foreach (string t_filter_reg in t_filter_arr)
                                {
                                    t_chapter_content = Regex.Replace(t_chapter_content, t_filter_reg, "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                                }


                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(t_chapter_content))
                    {
                        bool t_save_ret = NetSiteCatchManager.WriteBookContent(m_sys_config.BookIndexPath, t_chapter.BookId, t_chapter.ChapterId, t_chapter_content);

                        if (t_save_ret)
                        {
                            BookUrlInfo t_book_url = new BookUrlInfo();
                            t_book_url.ChapterListUrl = "";
                            m_db_mgr.UpdateChapter(t_chapter.BookId, t_chapter.ChapterId, t_chapter.ChapterUrl, t_book_url);
                            SendMsg2Client(string.Format("{0}收录到章节内容", t_chapter.ChapterName));
                        }

                        t_downloaded_count++;
                    }
                }
            }

            return t_downloaded_count;

            #endregion

        }

      

        private List<ChapterInfo> GetChapterList(string i_chapter_list_url)
        {
            List<ChapterInfo> t_list = new List<ChapterInfo>();

            try
            {
                string t_html = HttpHelper.GetHttpRequestStringByNUll_Post(i_chapter_list_url);

                if (!string.IsNullOrEmpty(t_html))
                {
                    MatchCollection t_ma = Regex.Matches(t_html, m_chapter_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_ma != null && t_ma.Count > 0)
                    {
                        foreach (Match t_mc in t_ma)
                        {
                            ChapterInfo t_chapter = new ChapterInfo();
                            t_chapter.ChapterUrl =NetSiteCatchManager.GetFullUrl(i_chapter_list_url, t_mc.Groups[1].Value.ToString());
                            t_chapter.ChapterName = t_mc.Groups[2].Value.ToString();

                            t_list.Add(t_chapter);
                        }
                    }
                }

                return t_list;
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取小说章节列表失败" + ex.ToString());
                return null;
            }

        }

        /// <summary>
        /// 搜索普通章节
        /// </summary>
        /// <param name="i_site"></param>
        /// <param name="i_chpater"></param>
        private string SearchNomalChapter(ChapterInfo i_chapter)
        {
            long t_book_id = 0;
            long t_chapter_id = 0;

            string t_chapter_url = string.Empty;

            string t_chapter_content = string.Empty;

            Match t_mc = Regex.Match(i_chapter.ChapterUrl, "(\\d+),(\\d+)\\.aspx", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (t_mc.Groups.Count > 1)
            {
                if (!long.TryParse(t_mc.Groups[1].Value.ToString(), out t_book_id))
                    t_book_id = 0;
                if (!long.TryParse(t_mc.Groups[2].Value.ToString(), out t_chapter_id))
                    t_chapter_id = 0;
            }

            if (t_chapter_id == 0)
                return string.Empty;

            try
            {
                t_chapter_url = string.Format("http://files.qidian.com/Author{0}/{1}/{2}.txt", (t_book_id % 8 + 1), t_book_id, t_chapter_id);

                t_chapter_content = HttpHelper.GetHttpRequestStringByNUll_Post(t_chapter_url);

                t_mc = Regex.Match(t_chapter_content, "document.write\\(\\'(.*?)\\'\\)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (t_mc != null && t_mc.Groups.Count > 0)
                {
                    t_chapter_content = t_mc.Groups[1].Value.ToString();
                }

                t_chapter_url = i_chapter.ChapterUrl;

                if (!string.IsNullOrEmpty(t_chapter_content))
                {
                    t_chapter_content = NetSiteCatchManager.ReplaceContent(t_chapter_content);
                    if (t_chapter_content.Length > 100)
                    {
                        bool t_save_ret = NetSiteCatchManager.WriteBookContent(m_sys_config.BookIndexPath, i_chapter.BookId, i_chapter.ChapterId, t_chapter_content);

                        if (t_save_ret)
                        {
                            BookUrlInfo t_book_url = new BookUrlInfo();
                            t_book_url.ChapterListUrl = "";
                            m_db_mgr.UpdateChapter(i_chapter.BookId, i_chapter.ChapterId, t_chapter_url, t_book_url);
                            SendMsg2Client(string.Format("{0}收录到章节内容", i_chapter.ChapterName));
                        }

                        return t_chapter_content;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("搜索普通章节失败" + ex.ToString());
            }

            return t_chapter_content;
        }

        /// <summary>
        /// 获取章节列表
        /// </summary>
        /// <param name="i_chapter_list_url"></param>
        /// <returns></returns>
        private List<ChapterInfo> GetChapterByDBUrl(string i_chapter_list_url)
        {
            if (string.IsNullOrEmpty(i_chapter_list_url))
                return null;

            try
            {
                string t_html = HttpHelper.GetHttpRequestStringByNUll_Post(i_chapter_list_url);

                if (string.IsNullOrEmpty(t_html))
                    return null;

                MatchCollection t_ma = Regex.Matches(t_html, "<a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>([^<>]*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                if (t_ma == null || t_ma.Count <= 0)
                    return null;

                List<ChapterInfo> t_chapter_list = new List<ChapterInfo>();


                foreach (Match t_mc in t_ma)
                {

                    ChapterInfo t_chapter = new ChapterInfo();

                    t_chapter.ChapterUrl = NetSiteCatchManager.GetFullUrl(i_chapter_list_url, t_mc.Groups[1].Value.ToString());
                    t_chapter.ChapterName = t_mc.Groups[2].Value.ToString();


                    t_chapter_list.Add(t_chapter);

                }

                return t_chapter_list;
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取章节列表失败" + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 搜索VIP章节
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_vip_chapter_list"></param>
        /// <returns></returns>
        private int SearchVIPChapter(long i_book_id, List<ChapterInfo> i_vip_chapter_list)
        {
            if (i_vip_chapter_list == null || i_vip_chapter_list.Count == 0)
                return 0;

            int t_downloaded_count = 0;

            //从普通网站搜索到的章节列表
            List<ChapterInfo> t_normal_chapter_list = null;

            //已下载的VIP章节
            List<ChapterInfo> t_downloaded_vip_chapter_list = null;

            BookUrlInfo t_book_url = null;
            try
            {

                List<BookUrlInfo> t_url_list = m_db_mgr.GetBookUrlByBookId(i_book_id);

                if (t_url_list != null && t_url_list.Count > 0)
                {
                    foreach (BookUrlInfo t_url in t_url_list)
                    {
                        if (string.IsNullOrEmpty(t_url.ChapterListUrl))
                            continue;

                        t_normal_chapter_list = GetChapterByDBUrl(t_url.ChapterListUrl);

                        t_downloaded_count = SearchVIPChapter(t_url, t_normal_chapter_list, i_vip_chapter_list, out t_downloaded_vip_chapter_list);

                        t_book_url = new BookUrlInfo();
                        t_book_url.BookId = t_url.BookId;
                        t_book_url.ChapterListUrl = t_url.ChapterListUrl;
                        t_book_url.Host = t_url.Host;
                        t_book_url.Bits = t_downloaded_count;
                        t_book_url.SartIndex = t_url.SartIndex;
                        t_book_url.StartSign = t_url.StartSign;
                        t_book_url.EndSign = t_url.EndSign;
                        m_db_mgr.AddBookUrl(t_book_url);

                        //如果超过5个章节没找到
                        if (Math.Abs(i_vip_chapter_list.Count - t_downloaded_count) > 5)
                        {
                            foreach (ChapterInfo t_downloaded_chapter in t_downloaded_vip_chapter_list)
                            {
                                i_vip_chapter_list.Remove(t_downloaded_chapter);
                            }

                            continue;
                        }


                    }
                }
                else
                {
                    int t_start_index = 0;
                    string t_start_sign = string.Empty;
                    string t_end_sign = string.Empty;
                    string t_chapter_url = string.Empty;
                    string t_chapter_list_url = string.Empty;

                    foreach (ChapterInfo t_chapter in i_vip_chapter_list)
                    {
                        //从百度去搜索章节内容
                        m_chapter_mgr.SearchChapterContent(
                            t_chapter.BookName,
                            t_chapter.ChapterName,
                            t_chapter.BookId,
                            t_chapter.ChapterId,
                            out t_start_index,
                            out t_start_sign,
                            out t_end_sign,
                            out t_chapter_url,
                            out t_chapter_list_url);

                        if (string.IsNullOrEmpty(t_chapter_list_url))
                            continue;

                        t_normal_chapter_list = GetChapterList(t_chapter_list_url);

                        t_book_url = new BookUrlInfo();
                        t_book_url.BookId = t_chapter.BookId;
                        t_book_url.ChapterListUrl = t_chapter_list_url;
                        t_book_url.Host = new Uri(t_chapter_list_url).Host;
                        t_book_url.Bits = t_downloaded_count;
                        t_book_url.SartIndex = t_start_index;
                        t_book_url.StartSign = t_start_sign;
                        t_book_url.EndSign = t_end_sign;

                        t_downloaded_count = SearchVIPChapter(t_book_url, t_normal_chapter_list, i_vip_chapter_list, out t_downloaded_vip_chapter_list);

                        if (t_downloaded_count <= 0)
                            continue;

                        t_book_url.Bits = t_downloaded_count;
                        m_db_mgr.AddBookUrl(t_book_url);

                        //如果超过5个章节没找到
                        if (Math.Abs(i_vip_chapter_list.Count - t_downloaded_count) > 5)
                        {
                            continue;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("搜索VIP章节失败" + ex.ToString());
            }

            return t_downloaded_count;
        }

        /// <summary>
        /// 搜索VIP章节
        /// </summary>
        /// <param name="i_book_url"></param>
        /// <param name="i__normal_chapter_list"></param>
        /// <param name="i_vip_chapter_list"></param>
        /// <returns></returns>
        private int SearchVIPChapter(BookUrlInfo i_book_url, List<ChapterInfo> i__normal_chapter_list, List<ChapterInfo> i_vip_chapter_list,out List<ChapterInfo> o_downloaded_vip_chapter_list)
        {
            o_downloaded_vip_chapter_list = new List<ChapterInfo>();

            if (i_vip_chapter_list == null || i_vip_chapter_list.Count == 0)
                return 0;

            string t_chapter_content = string.Empty;

            int t_downloaded_count = 0;

            if (string.IsNullOrEmpty(i_book_url.StartSign))
            {
                LogHelper.Info(string.Format("bookid={0} 不存在章节解析规则，退出VIP收录", i_vip_chapter_list[0].BookId));
                return 0;
            }

            int t_start_index = 0;
            string t_start_sign = string.Empty;
            string t_end_sign = string.Empty;
            string t_chapter_url = string.Empty;
            string t_chapter_list_url = string.Empty;

            bool t_find = false;

            foreach (ChapterInfo t_vip_chapter in i_vip_chapter_list)
            {
                t_find = false;
                foreach (ChapterInfo t_normal_chapter in i__normal_chapter_list)
                {
                    if (NetSiteCatchManager.CompareChapterName(t_vip_chapter.ChapterName, t_normal_chapter.ChapterName) == true)
                    {
                        //从百度去搜索章节内容
                        t_chapter_content = m_chapter_mgr.GetContentBySign(t_vip_chapter.BookId, t_vip_chapter.ChapterId,
                            t_normal_chapter.ChapterUrl, i_book_url.SartIndex, i_book_url.StartSign, i_book_url.EndSign);

                        if (!string.IsNullOrEmpty(t_chapter_content))
                        {
                            o_downloaded_vip_chapter_list.Add(t_vip_chapter);
                            t_downloaded_count++;
                        }
                        else
                        {
                            //从百度去搜索章节内容
                           t_chapter_content= m_chapter_mgr.SearchChapterContent(
                                t_vip_chapter.BookName,
                                t_vip_chapter.ChapterName,
                                t_vip_chapter.BookId,
                                t_vip_chapter.ChapterId,
                                out t_start_index,
                                out t_start_sign,
                                out t_end_sign,
                                out t_chapter_url,
                                out t_chapter_list_url);

                           if (!string.IsNullOrEmpty(t_chapter_content))
                           {
                               t_downloaded_count++;
                               if (!string.IsNullOrEmpty(t_chapter_list_url))
                               {
                                   BookUrlInfo t_book_url = new BookUrlInfo();
                                   t_book_url.BookId = t_vip_chapter.BookId;
                                   t_book_url.ChapterListUrl = t_chapter_list_url;
                                   t_book_url.Host = new Uri(t_chapter_list_url).Host;
                                   t_book_url.Bits = t_downloaded_count;
                                   t_book_url.SartIndex = t_start_index;
                                   t_book_url.StartSign = t_start_sign;
                                   t_book_url.EndSign = t_end_sign;
                                   t_book_url.Bits = 1;
                                   m_db_mgr.AddBookUrl(t_book_url);
                               }
                           }
                           
                        }
                        t_find = true;
                        break;
                    }
                }

                if (t_find == false)
                {
                    //从百度去搜索章节内容
                    t_chapter_content = m_chapter_mgr.SearchChapterContent(
                         t_vip_chapter.BookName,
                         t_vip_chapter.ChapterName,
                         t_vip_chapter.BookId,
                         t_vip_chapter.ChapterId,
                         out t_start_index,
                         out t_start_sign,
                         out t_end_sign,
                         out t_chapter_url,
                         out t_chapter_list_url);

                    if (!string.IsNullOrEmpty(t_chapter_content))
                    {
                        t_downloaded_count++;

                        if (!string.IsNullOrEmpty(t_chapter_list_url))
                        {
                            BookUrlInfo t_book_url = new BookUrlInfo();
                            t_book_url.BookId = t_vip_chapter.BookId;
                            t_book_url.ChapterListUrl = t_chapter_list_url;
                            t_book_url.Host = new Uri(t_chapter_list_url).Host;
                            t_book_url.Bits = t_downloaded_count;
                            t_book_url.SartIndex = t_start_index;
                            t_book_url.StartSign = t_start_sign;
                            t_book_url.EndSign = t_end_sign;
                            t_book_url.Bits = 1;
                            m_db_mgr.AddBookUrl(t_book_url);
                        }
                    }
                }

            }


            return t_downloaded_count;
        }

        /// <summary>
        /// 搜索小说章节内容
        /// </summary>
        /// <param name="i_book_name"></param>
        /// <param name="i_chapter_name"></param>
        /// <returns></returns>
        public string GetChapterContent(ChapterInfo i_chapter, out string o_chapter_url,out string o_chapter_list_url,out List<ChapterInfo> o_chapter_list)
        {
            lock (m_syn_obj)
            {
                o_chapter_url = string.Empty;
                o_chapter_list_url=string.Empty;
                o_chapter_list=null;

                string t_search_url = string.Format("http://www.baidu.com/s?wd={0}", HttpUtility.UrlEncode(string.Format("{0} {1}", i_chapter.BookName, i_chapter.ChapterName), Encoding.UTF8));

                string t_content = GetContent(i_chapter, t_search_url, out o_chapter_url, out o_chapter_list_url, out o_chapter_list);

                return t_content;
            }

        }

        public string GetContent(ChapterInfo i_chapter, string i_url, out string o_chapter_url,out string o_chapter_list_url,out List<ChapterInfo> o_chapter_list)
        {
            lock (m_syn_obj)
            {
                o_chapter_url = string.Empty;
                o_chapter_list_url = string.Empty;
                o_chapter_list = null;

                try
                {

                    string t_html = HttpHelper.GetHttpRequestStringByNUll_Post(i_url);

                    string t_url = string.Empty;

                    string t_content = string.Empty;

                    if (!string.IsNullOrEmpty(t_html))
                    {
                        MatchCollection t_ma = Regex.Matches(t_html, "<h3\\s*?class=[\'\"]?t[\'\"]?><a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>(.*?)</a>\\s*?</h3>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                        if (t_ma == null || t_ma.Count <= 0)
                            return string.Empty;

                        foreach (Match t_mc in t_ma)
                        {
                            if (t_mc.Groups.Count == 3)
                            {
                                t_url = t_mc.Groups[1].Value.ToString();

                                if (NetSiteCatchManager.IsPiraticSite(t_url) == false)
                                    continue;

                               // t_content = ParseContent(t_url, i_chapter.BookId, i_chapter.ChapterId,out o_chapter_list_url);

                                t_content = ParseContentByUrl(i_chapter.ChapterName, t_url, i_chapter.BookId, i_chapter.ChapterId, out o_chapter_list_url);

                                if (string.IsNullOrEmpty(o_chapter_list_url))
                                    continue;

                                o_chapter_list = GetChpaterList(i_chapter, o_chapter_list_url);

                                if (o_chapter_list == null || o_chapter_list.Count <= 0)
                                    continue;

                                o_chapter_url = t_url;
                                break;

                            }
                        }
                    }

                    return t_content;
                }
                catch (Exception ex)
                {
                    LogHelper.Error("百度搜索小说章节失败" + ex.ToString());
                    return string.Empty;

                }
            }
        }

        /// <summary>
        /// 获取章节列表
        /// </summary>
        /// <param name="i_chapter_list_url"></param>
        /// <returns></returns>
        private List<ChapterInfo> GetChpaterList(ChapterInfo i_chapter, string i_chapter_list_url)
        {
            if (string.IsNullOrEmpty(i_chapter_list_url))
                return null;

            try
            {
                string t_html = HttpHelper.GetHttpRequestStringByNUll_Post(i_chapter_list_url);

                if (string.IsNullOrEmpty(t_html))
                    return null;

                MatchCollection t_ma = Regex.Matches(t_html, "<a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>([^<>]*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                if (t_ma == null || t_ma.Count <= 0)
                    return null;

                List<ChapterInfo> t_chapter_list = new List<ChapterInfo>();

                bool t_checked = false;

                foreach (Match t_mc in t_ma)
                {
                    ChapterInfo t_chapter = new ChapterInfo();

                    t_chapter.ChapterUrl =NetSiteCatchManager.GetFullUrl(i_chapter_list_url, t_mc.Groups[1].Value.ToString());
                    t_chapter.ChapterName = t_mc.Groups[2].Value.ToString();

                    t_chapter_list.Add(t_chapter);

                    if (NetSiteCatchManager.CompareChapterName(i_chapter.ChapterName, t_chapter.ChapterName))
                        t_checked = true;
                }

                if (t_checked == false)
                    return null;

                return t_chapter_list;
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取章节列表失败" + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 解析章节内容
        /// </summary>
        /// <param name="i_chapter_url"></param>
        /// <param name="i_page_content"></param>
        /// <param name="i_chapter_content_reg"></param>
        /// <param name="i_chaper_content_filter"></param>
        /// <returns></returns>
        public string ParseContent(string i_chapter_url, long i_book_id, long i_chapter_id,out string o_chapter_list_url)
        {
            lock (m_syn_obj)
            {
                string t_chapter_content = string.Empty;

                o_chapter_list_url = string.Empty;

                try
                {
                    string t_html = HttpHelper.GetHttpRequestStringByNUll_Post(i_chapter_url);
                    if (!string.IsNullOrEmpty(t_html))
                    {
                        MatchCollection t_ma_c = Regex.Matches(t_html, m_chapter_content_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                        if (t_ma_c == null || t_ma_c.Count == 0)
                            return string.Empty;

                        foreach (Match t_mc in t_ma_c)
                        {
                            t_chapter_content = GetContentByMC(t_mc, i_chapter_url, i_book_id, i_chapter_id);

                            if (!string.IsNullOrEmpty(t_chapter_content))
                            {
                                MatchCollection t_url_list_ma = Regex.Matches(t_html, "<a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>[^<>]*?目录[^<>]*?</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                                o_chapter_list_url = GetChapterListUrl(i_chapter_url, t_url_list_ma);

                                break;
                            }
                        }

                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("获取页面内容失败" + ex.ToString());
                }

                if (string.IsNullOrEmpty(o_chapter_list_url))
                    return string.Empty;

                return t_chapter_content;
            }

        }

        private string GetChapterListUrl(string i_chapter_url, MatchCollection i_ma)
        {
            MatchCollection t_ma = Regex.Matches(i_chapter_url, "(\\d+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            if (i_ma == null && i_ma.Count <= 0)
                return string.Empty;

            string t_chapter_list_url =NetSiteCatchManager.GetFullUrl(i_chapter_url, i_ma[0].Groups[1].Value.ToString());

            if (t_ma != null && t_ma.Count > 0)
            {
                foreach (Match t_mc in i_ma)
                {
                    if (t_mc.Groups[1].Value.ToString().Contains(t_ma[0].Groups[1].ToString()))
                    {
                        t_chapter_list_url =NetSiteCatchManager.GetFullUrl(i_chapter_url, t_mc.Groups[1].Value.ToString());
                        break;
                    }
                }
            }

            return t_chapter_list_url;
        }


        private string GetContentByMC(Match i_mc,  string i_chapter_url, long i_book_id, long i_chapter_id)
        {
            lock (m_syn_obj)
            {
                string t_chapter_content = string.Empty;

                bool t_is_img = false;

                if (i_mc != null && i_mc.Groups.Count > 0)
                {
                    t_chapter_content = i_mc.Groups[1].Value.ToString();

                    //存在图片
                    MatchCollection t_ma = Regex.Matches(t_chapter_content, "<img[^<>]*?src=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                    foreach (Match t_mc_img in t_ma)
                    {
                        string t_img_url = NetSiteCatchManager.GetFullUrl(i_chapter_url, t_mc_img.Groups[1].Value.ToString());
                        //下载图片
                        string t_file_name = NetSiteCatchManager.DownLoadFile(t_img_url, m_sys_config.BookIndexPath, i_book_id, i_chapter_id);

                        if (string.IsNullOrEmpty(t_file_name))
                        {
                            Uri t_ch = new Uri(i_chapter_url);
                            Uri t_img = new Uri(t_img_url);
                            if (t_ch.Host != t_img.Host)
                            {
                                t_chapter_content = string.Empty;
                                break;
                            }
                            t_file_name = t_img_url;
                        }
                        t_is_img = true;
                        t_chapter_content = Regex.Replace(t_chapter_content, t_mc_img.Groups[1].Value.ToString(), t_file_name);
                    }

                    //非图片
                    t_chapter_content = Regex.Replace(t_chapter_content, "<a[^<>]*?>.*?</a>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                    if (t_is_img == false)
                    {
                        string t_temp_conent = Regex.Replace(t_chapter_content, "[【】（）a-zA-Z，！？(),!?;\\.；、……<>]", "", RegexOptions.IgnoreCase);
                        if (string.IsNullOrEmpty(t_temp_conent) || t_temp_conent.Length < 1000)
                            return string.Empty;
                    }

                }
                return t_chapter_content;
            }
        }


        /// <summary>
        /// 解析章节内容
        /// </summary>
        /// <param name="i_chapter_url"></param>
        /// <param name="i_page_content"></param>
        /// <param name="i_chapter_content_reg"></param>
        /// <param name="i_chaper_content_filter"></param>
        /// <returns></returns>
        public string ParseContentByUrl(string i_chapter_name, string i_chapter_url,long i_book_id,long i_chapter_id,out string o_chapter_list_url)
        {

            string t_chapter_content = string.Empty;

            o_chapter_list_url = string.Empty;

            try
            {
                string t_html =HttpHelper.GetHttpRequestStringByNUll_Post(i_chapter_url);

                //判断小说章节title是否包含章节名称
                Match t_mc = Regex.Match(t_html, "<[^<>]*?title[^<>]*?>(.*?)</title[^<>]*?>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (t_mc != null && t_mc.Groups.Count > 0)
                {
                    string t_chapter_name = t_mc.Groups[1].Value.ToString();
                    if (NetSiteCatchManager.CompareChapterName(i_chapter_name, t_chapter_name) == false)
                        return string.Empty;
                }

                //如果小说章节是图片
                MatchCollection t_img_ma = Regex.Matches(t_html, m_img_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (t_img_ma != null && t_img_ma.Count > 0)
                {
                    StringBuilder t_img_sb = new StringBuilder();

                    foreach (Match t_img_mc in t_img_ma)
                    {
                        t_img_sb.AppendFormat("<img src='{0}' border='0' /><br>", NetSiteCatchManager.GetFullUrl(i_chapter_url, t_img_mc.Groups[1].Value.ToString()));
                    }

                    //获取小说目录页面
                    Match t_url_list_mc = Regex.Match(t_html, "<a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>[^<>]*?目录[^<>]*?</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                    if (t_url_list_mc != null && t_url_list_mc.Groups.Count > 0)
                    {
                        o_chapter_list_url = t_url_list_mc.Groups[1].Value.ToString();
                        o_chapter_list_url = NetSiteCatchManager.GetFullUrl(i_chapter_url, o_chapter_list_url);
                    }


                    return t_img_sb.ToString();
                }



                string t_temp_html = string.Empty;

                if (!string.IsNullOrEmpty(t_html))
                {

                    MatchCollection t_ma_c = Regex.Matches(t_html, m_chapter_content_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_ma_c == null || t_ma_c.Count == 0)
                        return string.Empty;

                    for (int index = t_ma_c.Count - 1; index > 0; index--)
                    {
                        t_temp_html = t_html.Substring(t_ma_c[index].Index);

                        t_chapter_content = GetContentByHTML(t_temp_html, i_chapter_url,i_book_id,i_chapter_id);

                        if (!string.IsNullOrEmpty(t_chapter_content))
                        {
                            MatchCollection t_url_list_ma = Regex.Matches(t_html, "<a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>[^<>]*?目录[^<>]*?</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            o_chapter_list_url = GetChapterListUrl(i_chapter_url, t_url_list_ma);

                            t_chapter_content = FilterContent(t_chapter_content);
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取页面内容失败" + ex.ToString());
            }


            return t_chapter_content;


        }

        /// <summary>
        /// 过滤章节匹配的标签
        /// </summary>
        /// <param name="i_content"></param>
        /// <returns></returns>
        private string FilterContent(string i_content)
        {
            if (string.IsNullOrEmpty(i_content))
                return string.Empty;

            MatchCollection t_ma = Regex.Matches(i_content, m_html_tag_reg, RegexOptions.IgnoreCase);

            Stack<string> t_tack = new Stack<string>();

            foreach (Match t_c in t_ma)
            {
                if (t_c.Groups[0].Value.Contains("/")) //结束标签，弹出栈
                {
                    if (t_tack.Count == 0) //如果栈为空，找到了成对的闭合标签
                    {
                        i_content = i_content.Substring(0, t_c.Index);
                        break;
                    }
                    else
                    {
                        t_tack.Pop();
                        if (t_tack.Count == 0)
                        {
                            i_content = i_content.Substring(0, t_c.Index);
                            break;
                        }
                    }
                }
                else //非闭合标签 压入栈
                {
                    t_tack.Push(t_c.Groups[0].Value.ToString());
                }
            }

            return i_content;
        }


        private string GetContentByHTML(string i_html, string i_chapter_url,long i_book_id,long i_chapter_id)
        {
            if (string.IsNullOrEmpty(i_html))
                return string.Empty;

            i_html = FilterContent(i_html);

            //存在图片
            MatchCollection t_ma = Regex.Matches(i_html, "<img[^<>]*?src=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            bool t_is_img = false;

            Dictionary<string, string> t_img_dic = new Dictionary<string, string>();

            foreach (Match t_mc_img in t_ma)
            {
                string t_img_url = t_mc_img.Groups[1].Value.ToString();

                if (!t_img_dic.ContainsKey(t_img_url))
                {
                    t_img_dic.Add(t_img_url, t_img_url);
                }

                t_is_img = true;
            }

            if (t_img_dic.Count > 0)
            {
                foreach (string t_key in t_img_dic.Keys)
                {
                    string t_img_url = NetSiteCatchManager.GetFullUrl(i_chapter_url, t_key);
                    //下载图片
                    string t_file_name = NetSiteCatchManager.DownLoadFile(t_img_url, m_sys_config.BookIndexPath, i_book_id, i_chapter_id);

                    if (string.IsNullOrEmpty(t_file_name))
                    {
                        Uri t_ch = new Uri(i_chapter_url);
                        Uri t_img = new Uri(t_img_url);
                        if (t_ch.Host != t_img.Host)
                        {
                            i_html = string.Empty;
                            break;
                        }
                        t_file_name = t_img_url;
                    }
           
                    i_html = Regex.Replace(i_html, t_key, t_file_name);
                }
            }

            if (string.IsNullOrEmpty(i_html))
                return i_html;

            //非图片
            i_html = Regex.Replace(i_html, "<a[^<>]*?>.*?</a>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            if (t_is_img == false)
            {
                string t_temp_conent = Regex.Replace(i_html, "[【】（）a-zA-Z，！？(),!?;\\.；、……<>]", "", RegexOptions.IgnoreCase);
                if (string.IsNullOrEmpty(t_temp_conent) || t_temp_conent.Length < 1000)
                    return string.Empty;
            }

            if (!string.IsNullOrEmpty(i_html))
            {
                i_html = Regex.Replace(i_html, "<[/]*?div[^<>]*?>", "<br>", RegexOptions.IgnoreCase);
                i_html = Regex.Replace(i_html, "<script[^<>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            }

            return i_html;
        }
    }
}
